package com.itheima.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class FileUploadController {
    @RequestMapping("/fileupload")
    public String fileupload(MultipartFile file, HttpServletRequest request) throws IOException {
        //获取上传文件真实目录 获取项目所在目录
        String path = request.getServletContext().getRealPath("/");
        //上传目录
        String uploadPath = "/static/upload/";
        //上传文件名
        String fileName = file.getOriginalFilename();
        String filePath = path + uploadPath + fileName;
        if (!file.isEmpty()){
            file.   transferTo(new File(filePath));
            return "uploadSuccess";//显示成功
        }
        return "uploadFailuer";//显示失败
    }
    @RequestMapping("/uploads")
    public String uploads( MultipartFile[] files, HttpServletRequest request) throws IOException {
        String path = request.getServletContext().getRealPath("/");
        String uploadPath = "/static/upload/";
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            String filePath = path + uploadPath + fileName;
            try {
                file.transferTo(new File(filePath));
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    }
        return "uploadSuccess";
}
}
